/*
-------------------------------------------------------------------
BIE is Copyright 2001-2004 Brunswick Corp.
-------------------------------------------------------------------
Please read the legal notices (docs/legal.txt) and the license
(docs/bie_license.txt) that came with this distribution before using
this software.
-------------------------------------------------------------------

*/
/*
 * Load.java
 *
 * Created on December 3, 2002, 2:48 PM
 */

package tests.jd;

import com.webdeninteractive.toolkit.threadpool.*;
import java.util.*;
/**
 *
 * @author  jd
 */
public class Load2 {
/*    
Benchmarking scorpion.wdi.com (be patient).....done
Server Software:        Apache
Server Hostname:        scorpion.wdi.com
Server Port:            5803

Document Path:          /services/BIERequest
Document Length:        447 bytes

Concurrency Level:      1
Time taken for tests:   9.344 seconds
Complete requests:      10
Failed requests:        0
Broken pipe errors:     0
Total transferred:      5880 bytes
Total POSTed:           7040
HTML transferred:       4470 bytes
Requests per second:    1.07 [#/sec] (mean)
Time per request:       934.40 [ms] (mean)
Time per request:       934.40 [ms] (mean, across all concurrent requests)
Transfer rate:          0.63 [Kbytes/sec] received
                        0.75 kb/s sent
                        1.38 kb/s total

Connnection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0     0    0.0      0     0
Processing:    56   934  308.1   1029  1043
Waiting:       56   927  306.5   1029  1039
Total:         56   934  308.1   1029  1043

Percentage of the requests served within a certain time (ms)
  50%   1029
  66%   1029
  75%   1031
  80%   1039
  90%   1043
  95%   1043
  98%   1043
  99%   1043
 100%   1043 (last request)    
*/    
    private static int completed=0;
    private static int failed=0;
    private static long requestTime;
    private static long testStarted;
    private static long testCompleted;
    private static long transferred;
    
    public static void report(int id, long time,byte[] response) {                
        completed ++;
        requestTime += time;
        transferred += response.length;
        System.out.println("COMPLETED #" + id + " in " + time + " ms. Response=" + new String(response));
    }
    
    public static void report(int id, Exception e) {
        failed++;
        System.out.println("FAILED #" + id + " " + e.getMessage());        
    }
    
    private static int count = 0;
    
    private static synchronized int getCount() {
        count++;
        return count;
    }
    
    public static void main(String[] args) throws Exception {
        
        if (args.length != 6) {
            System.out.println("usage: java Load clientService routeKey uri message numRequests numConcurrent");
            return;
        }
                                        
        int numRequests = Integer.parseInt(args[4]);
        int connRequests = Integer.parseInt(args[5]);
        String clientService = args[0];
        String routeKey = args[1];
        String uri = args[2];
        
        ThreadPoolManager tpm = new ThreadPoolManager(connRequests,new LoadListener(),0);
                
        testStarted = new Date().getTime();
        for (int i=0;i<numRequests;i++) {            
            tpm.putTask(new LoadTask(clientService, routeKey,uri,args[3].getBytes(),getCount()));
        }
        
        int running = 0;
        int queued = 0;
        int waiting = 0;
        int stopped = 0;

        while (running!=0 || queued !=0 || waiting != connRequests) {
            Thread.sleep(2000);
            running =tpm.getRunning();
            queued = tpm.getQueued();
            waiting = tpm.getWaiting();
            stopped = tpm .getStopped();
            
            System.out.println("----------------------------------------------------");
            System.out.print("Running: " + running);
            System.out.print(" Queued: " + queued);
            System.out.print(" Waiting: " + waiting);
            System.out.println(" Stopped: " + stopped);        
            System.out.println("----------------------------------------------------");
            
        }
        testCompleted = new Date().getTime();
        
        System.out.println("\n\n----------------------------------------------------");
        System.out.println("RESULTS:");
        System.out.println("----------------------------------------------------");

        long testTime = testCompleted - testStarted;
        float requestsPer = ((float)completed)/((float)(testTime/1000));        
        long aveTime = requestTime/completed;
        float aveTransferred = ((float)completed)/((float)transferred); 
        
        System.out.println("\nURI: " + uri);
        System.out.println("Route ID: " + routeKey);
        System.out.println("Service: " + clientService);
        
        System.out.println("\nConcurrency Level:\t\t" + connRequests );
        System.out.println("Complete Requests:\t\t" + completed);
        System.out.println("Failed Requests:\t\t" + failed);
        System.out.println("Total Test Time:\t\t" + testTime + " ms" );
        System.out.println("Requests per second:\t" + requestsPer);
        System.out.println("Average Request Time:\t" + aveTime + " ms");               
        System.out.println("Total Bytes Transferred:\t" + transferred);
        System.out.println("Average Bytes Transferred:\t" + aveTransferred);
        return;
    }            
}
